﻿using Intex.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
namespace WPFExcelReport
{
    public partial class CAD2INTEXData : BaseFunction
    {
        public string ITXMANDANT = "00", ITXUSER = "INTEXMAP", ITXDATE = "20120723";
        public string ITXLOOMTYPE = "T610  ";
        public string ITXFART = "                    ", ITXGART = "                    ", ITXBART = "                    ";
        public string ITXFVAR = "  ", ITXGVAR = "  ", ITXBVAR = "1 ";
        public string ITXFCOLOR = "          ", ITXGCOLOR = "          ", ITXBCOLOR = "          ";
        public string ITXFFINISH = "          ", ITXCADID = "1";
        public string ITXFLEN = "1", ITXFUNIT = "M", ITXGLEN = "1", ITXBLEN = "1", ITXFGRATIO = "1", ITXGBRATIO = "1";

        public string CADCHAR = " ", ITXCHAR = " ";
        DataRow dwIntexStd = null;
        public Dictionary<string, object> CADTable = new Dictionary<string, object>();
        private Convert2Intex Convert2Intex; 
        public CADInterface.DSTCADDB SetCAD;
        string TrisInclude = "";
        GreigeMasterData m_GM;
        public CAD2INTEXData()
            : base()
        {
        }
        public CAD2INTEXData(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {
        }
        public CAD2INTEXData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        #region=============INTEX WS/BS=============================
        public DataSet ResetSetDispo(string setID = "")
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet settemp;
            try
            {
                ArrayList tables = new ArrayList();
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\PRODSETUP.xls");
                if (!System.IO.File.Exists(filePath))
                {
                    return setReturn;
                }
                if (exAgent == null)
                {
                    exAgent = new ExcelX.ExcelLib.ExcelAgent(filePath);
                }
                setResult = exAgent.LoadDataTableDef(filePath, "");
                filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\PRODSETUP.XML");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                setResult.WriteXml(filePath);
                setResult = new DataSet();
                setResult = LoadSetDispo(setID);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadSetDispo(string setID = "")
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\PRODSETUP.XML");
                if (!System.IO.File.Exists(filePath))
                {
                    return setReturn;
                }
                setResult.ReadXml(filePath);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public void SaveSetDispo(DataTable tbDispo)
        {
            DataSet setDispo = new DataSet();

            try
            {
                setDispo.Merge(tbDispo);
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\DATADISPO.XML");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                    setDispo.WriteXml(filePath);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion
        private bool INTEX_EXISTS(string art, string var, string color, string finish)
        {
            bool exist = false;
            try
            {
                DataSet setReturn = new DataSet();
                DataSet setResult = new DataSet();
                DataTable tbLocal = null;
                OracleCommand or1;
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT count(*) FROM ABPOS"
               + " WHERE ABPART=:ABPART AND WAKART=:WAKART AND WAKVAR=:WAKVAR AND WAKWCOL=:WAKWCOL";
                or1.Parameters.Add("ABPART", OracleType.NVarChar).Value = art;
                or1.Parameters.Add("ABPVAR", OracleType.NVarChar).Value = var;
                or1.Parameters.Add("ABPFARBE", OracleType.NVarChar).Value = color;
                or1.Parameters.Add("ABPBESCHICH", OracleType.NVarChar).Value = finish;

                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    tbLocal = setResult.Tables[0];
                    if (tbLocal.Rows.Count > 0)
                        exist = true;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return exist;
        }
        public DataSet ConvertToIntex(string FInterID, ref bool fail, ref string failreason)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string art, var, color, finish;
            string stage;
            string matchid = "";
            string yart, yvar, ycolor, ydesc;
            string apkrs = "", aplfd = "", apdes = "";
            DataSet setFlow = new DataSet();
            DataTable tbGSCOLOR = new DataTable();
            string expression = "";
            DataRow[] rows;
            try
            {
              
                Convert2Intex = new Convert2Intex(SetCAD, dwIntexStd,OracleDB,proxy);
                dwIntexStd = Convert2Intex.dwIntexStd; 
                art = dwIntexStd["ITXFART"].ToString();
                var = dwIntexStd["ITXFVAR"].ToString();
                color = dwIntexStd["ITXFCOLOR"].ToString();
                finish = dwIntexStd["ITXFFINISH"].ToString();
                // ITXFART ITXFVAR  ITXFCOLOR ITXFFINISH
                if (INTEX_EXISTS(art, var, color, finish))
                {
                    fail = true;
                    failreason = "该品种已经创建生产单";
                    return setReturn;
                }
                #region======FABRIC================
                setReturn.Merge(Convert2Intex.SetExt);
                setResult = Convert2WASTAMM();
                setReturn.Merge(setResult);
                setResult = Convert2WTSCHUSS();
                setReturn.Merge(setResult);
                setResult = Convert2WAFARBEN();
                setReturn.Merge(setResult);
                setResult = Convert2WAFINISH();
                setResult.Tables[0].TableName = "WAFARBEN";
                setReturn.Merge(setResult);
                setResult = Convert2WABIND();
                setReturn.Merge(setResult);
                setResult = Convert2WTEINZ();
                setReturn.Merge(setResult);

                setResult = Convert2WTGEW();
                setReturn.Merge(setResult);
                setResult = Convert2WASTPASS();
                setReturn.Merge(setResult);
                setResult = Convert2WTPASS();
                setReturn.Merge(setResult);

                setResult = Convert2WASTKE();
                setReturn.Merge(setResult);
                setResult = Convert2WASTGA();
                setReturn.Merge(setResult);
                setTemp = new DataSet();

                foreach (DataRow dwMaster in setReturn.Tables["WASTGA"].Rows)
                  {
                      yart=dwMaster["WAGGART"].ToString();
                      yvar=dwMaster["WAGGVAR"].ToString();
                      ycolor=dwMaster["WAGGFARBE"].ToString();
                      ydesc=dwMaster["WAGBEM"].ToString();
                      setResult = Convert2GSCOLOR(yart, yvar, ycolor, ydesc);
                      setTemp.Merge(setResult);
                  }
                setReturn.Merge(setTemp);
                stage = "F";
                setTemp = new DataSet();
                foreach (DataRow dwMaster in setReturn.Tables["WASTAMM"].Rows)
                {
                    art = dwMaster["WAART"].ToString();
                    var = dwMaster["WAVAR"].ToString();
                    matchid = finish;
                    setFlow = new DataSet();
                    setFlow = GetProcessFlow(stage, art, var, color, matchid);
                    if (setFlow.Tables.Count == 0)
                        continue;
                    if (setFlow.Tables[0].Rows.Count == 0)
                        continue;
                    foreach (DataRow dwFlow in setFlow.Tables[0].Rows)
                    {
                        setResult = Convert2APZUORD(stage, art, var, dwFlow);
                        setTemp.Merge(setResult);
                    }  
                }
                setReturn.Merge(setTemp);
                stage = "R";
                setTemp = new DataSet();
                foreach (DataRow dwMaster in setReturn.Tables["WASTAMM"].Rows)
                {
                    // WAVWART WAVWVAR

                    art = dwMaster["WAVWART"].ToString();
                    var = dwMaster["WAVWVAR"].ToString();
                    matchid = dwMaster["WARABLOM"].ToString().Trim(); //织机类型代码
                    setFlow = new DataSet();
                    setFlow = GetProcessFlow(stage, art, var, color, "");
                    if (setFlow.Tables.Count == 0)
                        continue;
                    if (setFlow.Tables[0].Rows.Count == 0)
                        continue;
                    foreach(DataRow dwFlow in setFlow.Tables[0].Rows)
                    {
                         setResult = Convert2APZUORD(stage, art, var, dwFlow);
                         setTemp.Merge(setResult);
                    }                   
                }
                setReturn.Merge(setTemp);

                #endregion

                #region======WARP================ 
                setResult = Convert2BSSATZ();
                setReturn.Merge(setResult);
                setResult = Convert2BHSATZ();
                setReturn.Merge(setResult);
                setResult = Convert2WTSAER();
                setReturn.Merge(setResult);
                DataSet   setLoom = new DataSet();
                setLoom = GetLoomType(ITXLOOMTYPE);
                string processedb = "";
                foreach (DataRow rlm in setLoom.Tables[0].Rows)
                {
                    string loomtype = rlm["KEY"].ToString();
                    string beamtype = rlm["BTYPE"].ToString();
                    if(processedb.Contains(loomtype.Trim()+"_"+beamtype+";"))
                        continue;
                    processedb += loomtype.Trim() + "_" + beamtype + ";";                 
                    
                    setResult = Convert2BSGROE(loomtype, beamtype);
                    setReturn.Merge(setResult);
                }                
                setResult = Convert2BSFARB();
                setReturn.Merge(setResult);
                setResult = Convert2BASTGA();
                setReturn.Merge(setResult);
                setResult = Convert2BASTGAD();
                setReturn.Merge(setResult);
                setTemp = new DataSet();
                if (setReturn.Tables.Contains("GSCOLOR"))
                {
                    tbGSCOLOR = setReturn.Tables["GSCOLOR"];
                }
                foreach (DataRow dwMaster in setReturn.Tables["BASTGA"].Rows)
                {
                    yart = dwMaster["BAGGART"].ToString();
                    yvar = dwMaster["BAGGVAR"].ToString();
                    ycolor = dwMaster["BAGGFARBE"].ToString();
                    ydesc = dwMaster["BAGBEM"].ToString();
                    //GSCART  GSCVAR GSCFARBE
                    expression = "GSCART='" + yart + "' AND GSCVAR='" + yvar + "' AND GSCFARBE='" + ycolor+"'";
                    rows = tbGSCOLOR.Select(expression);
                    if (rows.Length > 0)
                        continue;
                    setResult = Convert2GSCOLOR(yart, yvar, ycolor, ydesc);
                    setTemp.Merge(setResult);
                }
                setReturn.Merge(setTemp);

                stage = "B";
                setTemp = new DataSet();

                foreach (DataRow dwMaster in setReturn.Tables["BHSATZ"].Rows)
                {
                    art = dwMaster["BHKART"].ToString();
                    var = dwMaster["BHKVAR"].ToString();
                    setFlow = new DataSet();
                    matchid = "JS";
                    setFlow = GetProcessFlow(stage, art, var, color, matchid);
                    if (setFlow.Tables.Count == 0)
                        continue;
                    if (setFlow.Tables[0].Rows.Count == 0)
                        continue;
                    foreach (DataRow dwFlow in setFlow.Tables[0].Rows)
                    {
                        setResult = Convert2APZUORD(stage, art, var, dwFlow);
                        setTemp.Merge(setResult);
                    }  
                    setTemp.Merge(setResult);
                }
                setReturn.Merge(setTemp);
                #endregion
                #region======YARN================
                stage = "Y";
                setTemp = new DataSet();

                if (setReturn.Tables.Contains("GSCOLOR"))
                {
                    tbGSCOLOR = setReturn.Tables["GSCOLOR"];
                }
                foreach (DataRow dwMaster in tbGSCOLOR.Rows)
                {
                    //GSCART  GSCVAR GSCFARBE
                    //get only one code 
                    yart = dwMaster["GSCART"].ToString();
                    yvar = dwMaster["GSCVAR"].ToString();
                    ycolor = dwMaster["GSCFARBE"].ToString();
                    matchid = "YD";
                    setFlow = new DataSet();
                    setFlow = GetProcessFlow(stage, yart, yvar, ycolor, matchid);
                    if (setFlow.Tables.Count == 0)
                        continue;
                    if (setFlow.Tables[0].Rows.Count == 0)
                        continue;
                    foreach (DataRow dwFlow in setFlow.Tables[0].Rows)
                    {
                        setResult = Convert2APZUORD(stage, yart, yvar, dwFlow);
                        setTemp.Merge(setResult);
                    }  

                    matchid = "FLP";
                    setFlow = new DataSet();
                    setFlow = GetProcessFlow(stage, yart, yvar, ycolor, matchid);
                    if (setFlow.Tables.Count == 0)
                        continue;
                    if (setFlow.Tables[0].Rows.Count == 0)
                        continue;
                    foreach (DataRow dwFlow in setFlow.Tables[0].Rows)
                    {
                        setResult = Convert2APZUORD(stage, yart, yvar, dwFlow);
                        setTemp.Merge(setResult);
                    }  
                }
                setReturn.Merge(setTemp); 
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}